package com.ardf.controller;

import com.ardf.entity.dto.*;
import com.ardf.service.IResultService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@RestController
@RequestMapping("/ardf/result")
public class ResultController {

    private final IResultService resultService;
    public ResultController(IResultService resultService){
        this.resultService=resultService;
    }

    /**
     * 获取结果菜单
     */
    @GetMapping("/getResultMenu")
    public ResponseEntity<List<ResultMenuDto>> getResultMenu(@RequestParam("ruleId") Long ruleId){
        List<ResultMenuDto> list=resultService.getResultMenu(ruleId);
        return ResponseEntity.ok(list);
    }
    /**
     * 分页查询个人赛成绩信息
     */
    @GetMapping("/searchInfoPersonAll")
    public ResponseEntity<Page<GradeInfoPersonTableDto>> searchInfoPersonAll(@RequestParam("pageNum") Integer pageNum,
                                                                             @RequestParam("pageSize") Integer pageSize,
                                                                             @RequestParam("projectId") Long projectId,
                                                                             @RequestParam("groupId") Long groupId,
                                                                             @RequestParam(value = "searchAll",required = false) String searchAll){
        Page<GradeInfoPersonTableDto> page;
        if(searchAll!=null&&searchAll.length()>0){
            page=resultService.searchInfoPersonAll(pageNum,pageSize,projectId,groupId,searchAll);
        }else {
            page=resultService.searchInfoPersonAll(pageNum,pageSize,projectId,groupId,null);
        }
        return ResponseEntity.ok(page);
    }
    /**
     * 分页查询接力赛成绩信息
     */
    @GetMapping("/searchInfoRelayAll")
    public ResponseEntity<Page<GradeInfoRelayTableDto>> searchInfoRelayAll(@RequestParam("pageNum") Integer pageNum,
                                                                           @RequestParam("pageSize") Integer pageSize,
                                                                           @RequestParam("projectId") Long projectId,
                                                                           @RequestParam("groupId") Long groupId,
                                                                           @RequestParam(value = "searchAll",required = false) String searchAll){
        Page<GradeInfoRelayTableDto> page;
        if(searchAll!=null&&searchAll.length()>0){
            page=resultService.searchInfoRelayAll(pageNum,pageSize,projectId,groupId,searchAll);
        }else {
            page=resultService.searchInfoRelayAll(pageNum,pageSize,projectId,groupId,null);
        }
        return ResponseEntity.ok(page);
    }
    /**
     * 分页查询个人赛结果
     */
    @GetMapping("/searchResultPersonAll")
    public ResponseEntity<Page<GradeResultPersonTableDto>> searchResultPersonAll(@RequestParam("pageNum") Integer pageNum,
                                                                                 @RequestParam("pageSize") Integer pageSize,
                                                                                 @RequestParam(value = "searchAll", required = false) String searchAll,
                                                                                 @RequestParam("projectId") Long projectId,
                                                                                 @RequestParam("groupId") Long groupId,
                                                                                 @RequestParam("algorithmId") Long algorithmId){
        return ResponseEntity.ok(resultService.searchResultPersonAll(pageNum,pageSize,searchAll,projectId,groupId,algorithmId));
    }
    /**
     * 分页查询接力赛结果
     */
    @GetMapping("/searchResultRelayAll")
    public ResponseEntity<Page<GradeResultRelayTableDto>> searchResultRelayAll(@RequestParam("pageNum") Integer pageNum,
                                                                               @RequestParam("pageSize") Integer pageSize,
                                                                               @RequestParam(value = "searchAll", required = false) String searchAll,
                                                                               @RequestParam("projectId") Long projectId,
                                                                               @RequestParam("groupId") Long groupId,
                                                                               @RequestParam("algorithmId") Long algorithmId){
        return ResponseEntity.ok(resultService.searchResultRelayAll(pageNum,pageSize,searchAll,projectId,groupId,algorithmId));
    }

    /**
     * 计算成绩
     */
    @PostMapping("/calculateResult")
    public void calculateResult(@RequestParam("algorithmId") Long algorithmId,
                                @RequestParam("ruleId") Long ruleId){
        resultService.calculateResult(algorithmId,ruleId);
    }
    /**
     * 删除
     */
    @PostMapping("/delete")
    public void deleteByAlgorithmId(@RequestParam("algorithmId") Long algorithmId){
        resultService.deleteByAlgorithmId(algorithmId);
    }
    /**
     * 下载表格
     */
    @GetMapping("/downloadResultTable")
    public void downloadResultTable(HttpServletResponse response,@RequestParam("algorithmId") Long algorithmId,
                                    @RequestParam("projectId") Long projectId,
                                    @RequestParam("groupId") Long groupId,
                                    @RequestParam("isRelay") Boolean isRelay) throws IOException {
        resultService.downloadResultTable(response,algorithmId,projectId,groupId,isRelay);
    }
    /**
     * 上传图片
     */
    @PostMapping("/uploadPicture")
    public String uploadPicture(@RequestParam("file") MultipartFile file,
                              @RequestParam("gradeId") Long gradeId) throws IOException {
        return resultService.uploadPicture(file,gradeId);
    }
}
